/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;
import java.util.List;

import org.opu.pc.services.PublicationService;
import org.opu.pc.services.PublisherService;
import org.opu.pc.services.beans.AbstractBean;
import org.opu.pc.services.beans.PublicationBean;
import org.opu.pc.services.beans.PublisherBean;
import org.opu.pc.ui.client.MainView;

/**
 *
 * @author yura
 */
public class Delete extends AbstractAction
		implements PropertyChangeListener {

    /*
     *  We cannot perform this action more than one time.
     */
	
	private static final long serialVersionUID = 8375641134305793836L;

    /*
     * Injection of different fields was performed separated for normal startup
     * spring context.
     */

	private PublicationService publicationService;
    private PublisherService publisherService;

    public void setPublicationService(PublicationService publicationService) {
        this.publicationService = publicationService;
    }

    public void setPublisherService(PublisherService publisherService) {
        this.publisherService = publisherService;
    }

    @Override
    public String getName() {
        return "Delete";
    }

    @Override
    public void runAction(ActionEvent e) throws Exception {
        super.runAction(e);
        Collection<? extends AbstractBean> beans = mainView.getSelectedBeans();
        if (beans != null && !beans.isEmpty()){
            for (AbstractBean bean : beans) {
                if (bean instanceof PublisherBean){
                    publisherService.deletePublisher(bean.getId());
                } else if (bean instanceof PublicationBean){
                    publicationService.deletePublication(bean.getId());
                } else {
                    throw new IllegalArgumentException("Unknown data type: " + bean);
                }
            }
            mainView.update();
        }
    }

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List newValue = (List) evt.getNewValue();
			setEnabled(newValue != null && !newValue.isEmpty());
		}
	}

}
